﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace auxiliary_proj
{
    public partial class UCUnits : UserControl
    {
        public UCUnits()
        {
            InitializeComponent();
            this.dataGridUnits.CellContentDoubleClick += new DataGridViewCellEventHandler(dataGridUnits_CellContentDoubleClick);
            dataGridUnits.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige;
            viewUnits();
        }

        public void viewUnits()
        {
            string query = "SELECT unit_id, name, email, phone_number, Balance FROM UNIT";
            try
            {
                //bind data to grid
                dataGridUnits.DataSource = Query.select(query);
                for (int i = 0; i < dataGridUnits.Rows.Count; i++)
                {
                    dataGridUnits.Rows[i].Cells["Balance"].Value = balanceFormat(double.Parse(dataGridUnits.Rows[i].Cells["Balance"].Value.ToString()));
                }
            }
            catch (MySqlException exc) { MessageBox.Show(exc.Message); }
        }

        public static String balanceFormat(double num)
        {
            return String.Format("{0:C}", num);
        }
        private void dataGridUnits_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            FormUnitSummary exists = Application.OpenForms.OfType<FormUnitSummary>().FirstOrDefault();
            if (exists == null)
            {
                FormUnitSummary fus = new FormUnitSummary(dataGridUnits.Rows[e.RowIndex].Cells["UnitName"].Value.ToString(), (int)dataGridUnits.Rows[e.RowIndex].Cells["unit_id"].Value);
                fus.StartPosition = FormStartPosition.CenterScreen;
                fus.Show();
            }
            else
            {
                exists.Close();
                exists.Dispose();
                FormUnitSummary fus = new FormUnitSummary(dataGridUnits.Rows[e.RowIndex].Cells["UnitName"].Value.ToString(), (int)dataGridUnits.Rows[e.RowIndex].Cells["unit_id"].Value);
                fus.StartPosition = FormStartPosition.CenterScreen;
                fus.Show();
            }
        }

        private void cell_valueChanged(object sender, DataGridViewCellEventArgs e)
        {
            double balance;
            string output = dataGridUnits.Rows[e.RowIndex].Cells["balance"].Value.ToString();
            bool isBalance = double.TryParse(dataGridUnits.Rows[e.RowIndex].Cells["balance"].Value.ToString(), out balance);
            if (output.Equals(""))
            {
                MessageBox.Show("Input must not be empty!");
            }
            else if (!isBalance)
            {
                //not a decimal
                MessageBox.Show("Input must be a decimal value!");
            }
            else
            {
                string update = "UPDATE unit SET balance = '" + dataGridUnits.Rows[e.RowIndex].Cells[e.ColumnIndex].Value +
                                  "' WHERE unit_id = '" + dataGridUnits.Rows[e.RowIndex].Cells["unit_id"].Value + "'";
                Query.nonQuery(update);

            }
        }
    }
}

